package com.app.studyroom.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.R;
import com.app.studyroom.api.model.messageNotice.MessageItem;
import com.app.studyroom.mapper.MessageReadStatusMapper;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> {

    private final Context context;
    private final List<MessageItem> messageList;
    private OnDeleteListener deleteListener; // ⬅️ 删除监听接口
    private OnMessageClickListener listener;

    public interface OnDeleteListener {
        void onDelete(MessageItem item);
    }
    public interface OnMessageClickListener {
        void onClick(MessageItem item);
    }
    public void setOnDeleteListener(OnDeleteListener listener) {
        this.deleteListener = listener;
    }

    public MessageAdapter(Context context, List<MessageItem> messageList, OnMessageClickListener listener) {
        this.context = context;
        this.messageList = messageList;
        this.listener = listener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle, tvContent, tvTime, tvMessageStatus; // ⬅️ 添加删除按钮 TextView
        Button btnDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_message_title);
            tvContent = itemView.findViewById(R.id.tv_message_content);
            tvTime = itemView.findViewById(R.id.tv_message_time);
            tvMessageStatus = itemView.findViewById(R.id.tv_message_status);
            btnDelete = itemView.findViewById(R.id.btnDelete); // ⬅️ 对应布局中的删除按钮
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_message, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MessageItem item = messageList.get(position);
        holder.tvTitle.setText(item.getTitle());
        holder.tvContent.setText(item.getContent());
        holder.tvTime.setText(item.getCreateTime());

        String displayMessageStatus = MessageReadStatusMapper.getNameByCode(item.getStatus()+"");
        holder.tvMessageStatus.setText(displayMessageStatus);
        holder.btnDelete.setOnClickListener(v -> {
            if (deleteListener != null) {
                deleteListener.onDelete(item);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(item);
            }
        });

        if (item.getStatus() == 1) {
            // 已读：灰背景 + 灰字
            holder.itemView.setBackgroundColor(
                    ContextCompat.getColor(context, R.color.color_message_read_bg)
            );
            holder.tvTitle.setTextColor(ContextCompat.getColor(context, R.color.color_message_read_text));
            holder.tvContent.setTextColor(ContextCompat.getColor(context, R.color.color_message_read_text));
            holder.tvTime.setTextColor(ContextCompat.getColor(context, R.color.color_message_read_text));
            holder.tvMessageStatus.setTextColor(ContextCompat.getColor(context, R.color.color_message_read_text));
        } else {
            // 未读：透明背景 + 默认黑字（或你定义的颜色）
            holder.itemView.setBackgroundColor(
                    ContextCompat.getColor(context, R.color.color_message_unread_bg)
            );

        }
    }

    @Override
    public int getItemCount() {
        return messageList.size();
    }
}
